package com.wwwc.index.web.servlet;

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
import java.lang.Math.*;

public class StockRecordOpration {
    Vector vmb = null;
    Vector vms = null;
    Vector vhb = null;
    Vector vhs = null;

    public void setRecordVector(Vector vbr, Vector vsr) {
	vmb = new Vector();
	vms = new Vector();
	vhb = new Vector();
	vhs = new Vector();

	try {
	    StockRecord rb = null;
	    StockRecord rs = null;
	    Enumeration enum = null;

	    if(vbr == null || vbr.isEmpty()) {
		enum = vsr.elements();
		while(enum.hasMoreElements()) {
		    rs = new StockRecord((Vector)enum.nextElement());
		    vhs.add(rs);
		}
		return;
	    }

	    if(vsr == null || vsr.isEmpty()) {
		enum = vbr.elements();
		while(enum.hasMoreElements()) {
		    rb = new StockRecord((Vector)enum.nextElement());
		    vhb.add(rb);
		}
		return;
	    }

	    rb = null;
	    rs = null;

	    Enumeration enumb = null;
	    Enumeration enums = null;

	    final Stack stkb = new Stack();
	    final Stack stks = new Stack();

	    enumb = vbr.elements();
	    while(enumb.hasMoreElements()) {
		rb = new StockRecord((Vector)enumb.nextElement());
		stkb.push(rb);
	    }

	    enums = vsr.elements();
	    while(enums.hasMoreElements()) {
		rs = new StockRecord((Vector)enums.nextElement());
		stks.push(rs);
	    }

	    int bs = 0;
	    int ss = 0;

	    StockRecord rm = null;

	    while (!stkb.empty() || !stks.empty()) {
		rb =(StockRecord)stkb.pop();
		rs =(StockRecord)stks.pop();
		
		bs = rb.getShare();
		ss = rs.getShare();
		if(bs == ss) {
		    vmb.add(rb);
		    vms.add(rs);
		}
		else {
		    rm = subtractRecords(rb, rs);
		    if((rm.getBuySell()).equals("B")) {
			vms.add(rs);
			vmb.add(subtractRecords(rb, rm));
			stkb.push(rm);
		    }
		    if((rm.getBuySell()).equals("S")) {
			vmb.add(rb);
			vms.add(subtractRecords(rs, rm));
			stks.push(rm);
		    }
		}

		if(stks.empty()) {
		    while(!stkb.empty()) {
			rb =(StockRecord)stkb.pop();
			vhb.add(rb);
		    }
		}
		
		if(stkb.empty()) {
		    while(!stks.empty()) {
			rs =(StockRecord)stks.pop();
			vhs.add(rs);
		    }
		}
	    }
	}
	catch (Exception e) {
	    System.out.println("StockRecordOpration:"+e);
	}

	/*
	StockRecord rb = null;
	StockRecord rs = null;
	StockRecord rn = null; // crate a new record to match sold record
	StockRecord rm = null; // more left from bought record

	try {
	    Enumeration enum = null;
	    if(vbr == null || vbr.isEmpty()) {
		enum = vsr.elements();
		while(enum.hasMoreElements()) {
		    rs = new StockRecord((Vector)enum.nextElement());
		    vhs.add(rs);
		}
		return;
	    }

	    if(vsr == null || vsr.isEmpty()) {
		enum = vbr.elements();
		while(enum.hasMoreElements()) {
		    rb = new StockRecord((Vector)enum.nextElement());
		    vhb.add(rb);
		}
		return;
	    }

	    //=======================================================
	    // put all bought record into stack
	    //=======================================================

	    final Stack stk = new Stack();
	    enum = vbr.elements();
	    while(enum.hasMoreElements()) {
		rb = new StockRecord((Vector)enum.nextElement());
		if((rb.getTick()).equals("IDNX")) {
		    System.out.println("AAAAAAA==["+rb.getDate()+"]["+rb.getShare()+"]");
		}
		stk.push(rb);
	    }
	    
	    int bs  = 0; //bought shares
	    int bms = 0;
	    int ss  = 0;
	    double bc  = 0.0;
	    double bmc = 0.0;
	    double sc  = 0.0;
	    
	    enum = vsr.elements();
	    while(enum.hasMoreElements()) {
		bs  = 0;
		bms = 0;
		ss  = 0;
		bc  = 0.0;
		bmc = 0.0;
		sc  = 0.0;

		rs = new StockRecord((Vector)enum.nextElement());
		if((rs.getTick()).equals("IDNX")) {
		    System.out.println("SSSSSSSSSS=["+rs.getShare()+"]["+rs.getDate()+"]");
		}
		ss = rs.getShare();
		sc = Math.abs(rs.getCost());

		if(stk.empty()) {
		    vhs.add(rs);
		    continue;
		}		
		
		rb = (StockRecord)stk.pop();

		bs = rb.getShare();
		bc = Math.abs(rb.getCost());

		if((rb.getTick()).equals("IDNX")) {
		    System.out.println("BBBBBBBB=["+rb.getShare()+"]["+rb.getDate()+"]");
		}

		if(bs == ss) {
		    vmb.add(rb);
		    vms.add(rs);
		    continue;
		}

		if(bs>ss) {
		    rn = new StockRecord();
		    rn.setBuySell(rb.getBuySell());
		    rn.setTick(rb.getTick());
		    rn.setDate(rb.getDate());
		    rn.setPrice(rb.getPrice());
		    rn.setShare(ss);
		    rn.setCost((bc*ss/bs));
		    
		    vmb.add(rn);
		    vms.add(rs);

		    rm = new StockRecord();
		    rm.setBuySell(rb.getBuySell());
		    rm.setTick(rb.getTick());
		    rm.setDate(rb.getDate());
		    rm.setPrice(rb.getPrice());
		    rm.setShare(bs-ss);
		    rm.setCost((bc*(bs-ss)/bs));
		    stk.push(rm);
		    continue;
		}

		if(bs<ss) {
		    rn = new StockRecord();
		    while(bs<=ss && !stk.empty()) {
			rm = new StockRecord();
			rm = (StockRecord)stk.pop();
			bms = rm.getShare();
			bmc = Math.abs(rm.getCost());
			
			bs = bs + bms;
			bc = bc + bmc;
		    }

		    if(bs==ss) {
			rn.setBuySell(rm.getBuySell());
			rn.setTick   (rm.getTick());
			rn.setDate   (rm.getDate());
			rn.setPrice  (rm.getPrice());
			rn.setShare  (ss);
			rn.setCost   (bc);

			vmb.add(rn);
			vms.add(rs);
		    }

		    if(bs>ss) {
			rn.setBuySell(rm.getBuySell());
			rn.setTick   (rm.getTick());
			rn.setDate(  rm.getDate());
			rn.setPrice  (rm.getPrice());
			rn.setShare  (ss);
			rn.setCost   (((bc-bmc)+bmc*(bms-(bs-ss))/bms)+"");

			vmb.add(rn);
			vms.add(rs);

			rm.setShare(bs-ss);
			rm.setCost(bmc*(bs-ss)/bms);
			stk.push(rm);
		    }
		    
		    if(bs<ss) {
			if(bms>0) {
			    rn.setBuySell(rm.getBuySell());
			    rn.setTick   (rm.getTick());
			    rn.setDate(  rm.getDate());
			    rn.setPrice  (rm.getPrice());
			    rn.setShare(bs);
			    rn.setCost(bc);

			    vmb.add(rn);

			    rn = new StockRecord();
			    rn.setBuySell(rs.getBuySell());
			    rn.setTick   (rs.getTick());
			    rn.setDate(  rs.getDate());
			    rn.setPrice  (rs.getPrice());
			    rn.setShare(bs); //???????????????
			    rn.setCost(sc*bs/ss);

			    vms.add(rn);
			}
		    
			rm = new StockRecord();
			rm.setBuySell(rs.getBuySell());
			rm.setTick   (rs.getTick());
			rm.setDate(  rs.getDate());
			rm.setPrice  (rs.getPrice());
			rm.setShare(ss-bs);
			rm.setCost(sc*(ss-bs)/ss);
			
			vhs.add(rm);
		    }
		}
	    }

	    while(!stk.empty()) {
		rm = new StockRecord();
		rm = (StockRecord)stk.pop();
		vhb.add(rm);
	    }
	
	}
	catch (Exception e) {
	    System.out.println("StockRecordOpration:Error:"+e);
	}
	*/
    }

    private StockRecord subtractRecords(StockRecord r1, StockRecord r2) {
	StockRecord r = new StockRecord();
	double c1 = java.lang.Math.abs(r1.getCost());
	double c2 = java.lang.Math.abs(r2.getCost());
	int s1 = r1.getShare();
	int s2 = r2.getShare();
	double p1 = r1.getPrice();
	double p2 = r2.getPrice();
	int d1 = r1.getDateIntValue();
	int d2 = r2.getDateIntValue();
	double p = 0.0;
	if(s1>s2) {
	    r.setTick(r1.getTick());
	    r.setBuySell(r1.getBuySell());
	    r.setShare(s1-s2);
	    r.setPrice(p1);
	    r.setCost(java.lang.Math.abs(c1*(s1-s2)/s1));
	    r.setPrice(p1);
	    r.setDate(d1);
	}

	if(s1<s2) {
	    r.setTick(r2.getTick());
	    r.setBuySell(r2.getBuySell());
	    r.setShare(s2-s1);
	    r.setPrice(p2);
	    r.setCost(java.lang.Math.abs(c2*(s2-s1)/s2));
	    r.setPrice(p2);
	    r.setDate(d2);
	}

	if(s1==s2) {
	    return null;
	}
	return r;
    }

    public Vector getMatchedBRecordVector() {
	return vmb;
    }

    public Vector getMatchedSRecordVector() {
	return vms;
    }

    public Vector getHoldingLongRecordVector() {
	return vhb;
    }

    public Vector getHoldingShortRecordVector() {
	return vhs;
    }
}
